﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Huirui.Cavan.Presentation.Seedwork;
using Huirui.Cavan.Presentation.ViewModel.StockModule;
using Huirui.Cavan.Application.MainBoundedContext.StockModule;
using Huirui.Cavan.Domain.MainBoundedContext.StockModule;
using Huirui.Cavan.Domain.MainBoundedContext.SearchModule;
using System.Web.Mvc;
using Huirui.Cavan.Domain.Seedwork;
using AutoMapper;

namespace Huirui.Cavan.Presentation.Controller.StockModule
{
    public class StockInitialBalanceController : EntityController<StockInitialBalance, long, StockInitialBalanceSearch, IStockInitialBalanceViewModel, IStockInitialBalanceAppService>, IStockInitialBalanceController
    {
        public override string ControllerName
        {
            get { return ControllerConst.STOCKINITIALBALANCE; }
        }

        public override bool UseDenied
        {
            get { return false; }
        }

        public StockInitialBalanceController()
        {
            this.Model = ModelFactory.CreateModel<IStockInitialBalanceViewModel>();
        }

        [ActionName(ActionConst.ADD)]
        [HttpGet]
        [ActionPermissionAttribute(Name = "stockinitialbalance_query")]
        public ActionResult AddStockInitialBalance()
        {
            return View("~/Views/Stock/v1/addStockInitialBalance.cshtml");
        }

        [ActionName(ActionConst.ADD)]
        [HttpPost]
        [ActionPermissionAttribute(Name = "stockinitialbalance_edit")]
        public JsonResult AddAccount(StockInitialBalanceViewModel model)
        {
            BoolResult jsonResult = null;
            BoolGenericResult<StockInitialBalance> result = this.AppService.Add(model.Item);
            jsonResult = new BoolResult(result.IsSuccess, result.Messages);

            return Json(jsonResult, "application/json");
        }

        [ActionName(ActionConst.LIST)]
        [ActionPermissionAttribute(Name = "stockinitialbalance_query")]
        public JsonResult GetStockInitialBalances()
        {
            StockInitialBalanceJsonViewModel json = new StockInitialBalanceJsonViewModel();
            json.rows = new List<StockInitialBalanceJson>();

            IList<StockInitialBalance> stockInitialBalances = this.AppService.GetBySearch(new StockInitialBalanceSearch()).ToList();
            foreach (StockInitialBalance sib in stockInitialBalances)
            {
                StockInitialBalanceJson sibj = Mapper.Map<StockInitialBalance, StockInitialBalanceJson>(sib);
                json.rows.Add(sibj);
            }
            json.total = stockInitialBalances.Count;

            return Json(json, "application/json");
        }
    }
}
